<?php
include_once 'controller/default.php';
include_once 'connection/connection_database.php';

class banner {

    function Insert($banner) {
        $b = new banner();
        $link = "http://".$banner["banner_link"];
        $matudong = $b->MaTuDongTang();
        if($banner["banner_position"] > 0){
            $matudong["order"]++;
            $sql = "INSERT INTO  banner(banner_url, banner_display, banner_order, banner_link, banner_status) VALUES('{$banner["bannerurl"]}',{$banner["banner_position"]}, {$matudong["order"]}, '{$link}', {$banner["banner_display"]})";
        }else{
            $sql = "INSERT INTO  banner(banner_url, banner_display, banner_link, banner_status) VALUES('{$banner["bannerurl"]}',{$banner["banner_position"]}, '{$link}', {$banner["banner_display"]})";
        }
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Delete($banner) {
        $sql = "DELETE FROM  banner WHERE banner_id='{$banner["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function Update($banner) {
        $link = "http://".$banner["banner_link"];
        $sql = "UPDATE  banner SET banner_url='{$banner["bannerurl"]}', banner_display='{$banner["banner_position"]}', banner_link='{$link}', banner_status='{$banner["banner_display"]}' WHERE banner_id='{$banner["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function UpdateRight($banner) {
        $boolean = false;
        foreach ($banner["banner_value"] as $_banner) {
            //$bId = json_decode(json_decode('"' . $_banner . '"'), true);
			$bId = json_decode($_banner, true);
            $sql = "UPDATE  banner SET banner_order='{$bId["order"]}' WHERE banner_id='{$bId["banner_id"]}'";
            $result = mysql_query($sql) or die(mysql_error());
            if($result){
                $boolean = true;
            } else {
                return false;
            }
        }
        return $boolean;
    }
        
    function FillAllByTop() {
        $sql = "SELECT * FROM  banner WHERE banner_display = 0";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }

    function FillAllByRight() {
        $sql = "SELECT * FROM  banner WHERE banner_display = 1 ORDER BY banner_order ASC";
        $result = mysql_query($sql) or die(mysql_error());
        return $result;
    }
    
    function MaTuDongTang(){
        $sql = "SELECT MAX(banner_order) as 'order' FROM banner WHERE banner_display =1";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }
    
    function FillOne($banner) {
        $sql = "SELECT * FROM  banner WHERE banner_id='{$banner["id"]}'";
        $result = mysql_query($sql) or die(mysql_error());
        return mysql_fetch_array($result);
    }

}



?>
